/**
 * The ElderhealthProperties class simply contains the methods necessary to
 * lookup a properties file and then store the values from that file in a
 * java Properties object for lookup by other classes. This is a singleton
 * class.
 *
 * @author Jeremiah Weeden
 * @version 4/25/2007
 **/

package com.elderhealth.util;

import java.lang.String;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.*;

public class ElderhealthProperties extends Properties
{
   /* Instance Variables */
   private static ElderhealthProperties instance;
   public static final String defaultPropertiesFile =
       "elderhealth.properties";

   /**
    * ElderhealthProperties constructor loads the System properties and the
    * Elderhealth properties when an object is instantiated. It loads the
    * properties from a defaultPropertiesFile location.
    * The constructor is protected to prevent unwanted instantiation.
    *
    **/
   protected ElderhealthProperties()
   {
     super (System.getProperties());
     try
     {
       FileInputStream in = new FileInputStream(defaultPropertiesFile);
       load(in);
       in.close();
     }
     catch(IOException e)
     {
       /* Reporting a fatal error ends this program.  If the properties
        * file can not be read, the properties necessary to run this
        * program will not be defined.
        */
       System.out.println("Error reading " + defaultPropertiesFile + ":" +
           e.toString() + ". Properties file not loaded.");
     }
   }

   /**
    * ElderhealthProperties constructor loads the System properties
    * and the Elderhealth properties specified at the location filename when an
    * object is instantiated. The constructor is protected to prevent unwanted
    * instantiation.
    *
    * @param     filename
    **/
   protected ElderhealthProperties(String filename)
   {
     super (System.getProperties());
     try
     {
       FileInputStream in = new FileInputStream(filename);
       load(in);
       in.close();
     }
     catch(IOException e)
     {
       /* Reporting a fatal error ends this program.  If the properties
        * file can not be read, the properties necessary to run this
        * program will not be defined.
        */
       System.out.println("Error reading " + filename + ":" + e.toString() +
           ". Properties file not loaded.");
     }
   }

   /* Static Method Section */

   /**
    * getInstance the current instance of properties
    *
    * @return    ElderhealthProperties
    **/
   public static ElderhealthProperties getInstance()
   {
     if (null == instance)
     {
       instance = new ElderhealthProperties();
     }
     return instance;
   }

   /**
    * Public static method getInstance returns a single properties instance
    * created from this class. This getInstance has an argument where the
    * argument is the filename of the properties file.
    *
    * @param     filename
    * @return    ElderhealthProperties
    **/
   public static ElderhealthProperties getInstance(String filename)
   {
     if (null == instance)
     {
	 instance = new ElderhealthProperties(filename);
     }
     return instance;
   }
}
